package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TippedArrowItem;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

import java.util.Random;

public class BurningFeather extends SkillBookType {

    public BurningFeather() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 27 * 1000);

        if (!playerEntity.level.isClientSide) {
            ItemStack itemstack = new ItemStack(ItemRegisterManager.jian3.get());
            ArrowItem arrowitem = (ArrowItem) itemstack.getItem();
            AbstractArrowEntity abstractarrowentity = arrowitem.createArrow(playerEntity.level, itemstack, playerEntity);
            abstractarrowentity.pickup = AbstractArrowEntity.PickupStatus.DISALLOWED;
            SkillUtils.addEntity(abstractarrowentity,playerEntity.level);
            abstractarrowentity.shootFromRotation(playerEntity, playerEntity.xRot, playerEntity.yRot, 0.0F, 1 * 3.0F, 1.0F);
        }

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "BurningFeather";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.26");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.26");
    }

    public static class MarkArrow2 extends TippedArrowItem {
        public MarkArrow2() {
            super(new Properties());
        }

        @Override
        public AbstractArrowEntity createArrow(World p_200887_1_, ItemStack p_200887_2_, LivingEntity p_200887_3_) {
            ArrowEntity arrowentity = new MarkArrowentity2(p_200887_1_, p_200887_3_);
            arrowentity.setRemainingFireTicks(20*20);
            return arrowentity;
        }

        public static class MarkArrowentity2 extends ArrowEntity {

            public MarkArrowentity2(World p_i46758_1_, LivingEntity p_i46758_2_) {
                super(p_i46758_1_, p_i46758_2_);
            }

            private final long time = System.currentTimeMillis();

            @Override
            public void tick() {
                super.tick();
                if (System.currentTimeMillis()-time>20*1000) {
                    this.remove();
                }
            }

            @Override
            protected ItemStack getPickupItem() {
                return ItemStack.EMPTY;
            }

            @Override
            public int getColor() {
                return 16284963;
            }

            @Override
            protected void onHitBlock(BlockRayTraceResult blockRayTraceResult) {
                super.onHitBlock(blockRayTraceResult);
                MarkArrowentity2 that = this;
                SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
                    @Override
                    public void run() {
                        if (that.level.isClientSide) {
                            return;
                        }
                        BlockPos blockPos = blockRayTraceResult.getBlockPos();
                        Random random = new Random(System.currentTimeMillis());
                        long time = System.currentTimeMillis();
                        while (System.currentTimeMillis()-time<7000) {
                            int x = random.nextInt(12)-6 + blockPos.getX();
                            int z = random.nextInt(12)-6 + blockPos.getZ();
                            if (random.nextInt(100)<60) {
                                continue;
                            }
                            ItemStack itemstack = new ItemStack(ItemRegisterManager.jian4.get());
                            Arrow3 item = (Arrow3) itemstack.getItem();
                            AbstractArrowEntity abstractarrowentity = item.createArrow(that.level, itemstack, x, blockPos.getY() + 20, z);
                            abstractarrowentity.pickup = AbstractArrowEntity.PickupStatus.DISALLOWED;
                            SkillUtils.addEntity(abstractarrowentity,that.level);
                            Direction direction = Direction.DOWN;
                            abstractarrowentity.shoot(direction.getStepX(), (float) direction.getStepY() + 0.1F, direction.getStepZ(), 1, 0);
                            SkillUtils.sleep(100L);

                        }
                    }
                });
                this.remove();
            }

            @Override
            protected void onHitEntity(EntityRayTraceResult blockRayTraceResult) {
                super.onHitEntity(blockRayTraceResult);
                MarkArrowentity2 that = this;
                SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
                    @Override
                    public void run() {
                        if (that.level.isClientSide) {
                            return;
                        }
                        BlockPos blockPos = blockRayTraceResult.getEntity().blockPosition();
                        Random random = new Random(System.currentTimeMillis());
                        long time = System.currentTimeMillis();
                        while (System.currentTimeMillis()-time<7000) {
                            int x = random.nextInt(12)-6 + blockPos.getX();
                            int z = random.nextInt(12)-6 + blockPos.getZ();
                            if (random.nextInt(100)<60) {
                                continue;
                            }
                            ItemStack itemstack = new ItemStack(ItemRegisterManager.jian4.get());
                            Arrow3 item = (Arrow3) itemstack.getItem();
                            AbstractArrowEntity abstractarrowentity = item.createArrow(that.level, itemstack, x, blockPos.getY() + 20, z);
                            abstractarrowentity.pickup = AbstractArrowEntity.PickupStatus.DISALLOWED;
                            SkillUtils.addEntity(abstractarrowentity,that.level);
                            Direction direction = Direction.DOWN;
                            abstractarrowentity.shoot(direction.getStepX(), (float) direction.getStepY() + 0.1F, direction.getStepZ(), 1, 0);
                            SkillUtils.sleep(100L);

                        }
                    }
                });
                this.remove();
            }
        }

    }

    public static class Arrow3 extends ArrowItem {
        public Arrow3() {
            super(new Properties());
        }

        public AbstractArrowEntity createArrow(World p_200887_1_, ItemStack p_200887_2_, double x,double y, double z) {
            ArrowEntity arrowentity = new Arrowentity3(p_200887_1_, x,y,z);
            arrowentity.setRemainingFireTicks(20*20);
            return arrowentity;
        }

        public static class Arrowentity3 extends ArrowEntity {

            public Arrowentity3(World p_i46758_1_, double x, double y, double z) {
                super(p_i46758_1_, x, y, z);
            }

            private final long time = System.currentTimeMillis();

            @Override
            protected void doPostHurtEffects(LivingEntity p_184548_1_) {
                super.doPostHurtEffects(p_184548_1_);
                p_184548_1_.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SLOWDOWN,300,2));
            }

            @Override
            protected ItemStack getPickupItem() {
                return ItemStack.EMPTY;
            }

            @Override
            protected void onHitBlock(BlockRayTraceResult blockRayTraceResult) {
                super.onHitBlock(blockRayTraceResult);
                this.remove();
            }
        }


    }
}
